package sisicotren.main;

import sisicotren.model.simulator.Engine;

public class EngineThread extends java.util.Observable implements Runnable {

	public EngineThread(Engine engine, Long totalSeconds) {
		this.engine = engine;
		this.seconds = totalSeconds;
	}
	
	public EngineThread(Engine engine) {
		this.engine = engine;
		this.seconds = -1;
	}
	
	@Override
	public void run() {
		if(seconds > 0) {
			engine.run(seconds);
			engine.finish();
			engine.waitForAllQueuesInStateEmpty();
			engine.waitForAllChannelsInStateEmpty();
			engine.waitToDispatchAllClients();
			engine.offChannels();
		}
		else {
			engine.run();
			engine.waitForAllQueuesInStateEmpty();
			engine.waitForAllChannelsInStateEmpty();
			engine.waitToDispatchAllClients();
			engine.offChannels();
		}
		this.setChanged();
		this.notifyObservers();
	}
	
	public Integer getTotalClientsServed() {
		return engine.getTotalServedClients();
	}
	
	public Integer getTotalClientsInjected() {
		return engine.getInjectedClients();
	}
	
	private long seconds;
	private Engine engine;
}
